<!--
 * @Author: wangjh wangjiahui0710@163.com
 * @Date: 2024-06-11 11:33:52
 * @LastEditors: wangjh wangjiahui0710@163.com
 * @LastEditTime: 2024-07-18 19:04:52
 * @FilePath: \cip-admin\src\pages\park\dual-prevention\investigation-standards\components\sheet.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<!-- 表格组件 -->
<script lang="tsx" setup>
import { toRaw } from "vue";
import { onBeforeUnmount } from "vue";

import type { TableColumnOption } from "@/components/table-widget/table-widget.vue";
import TableWidget from "@/components/table-widget/table-widget.vue";
import { whManagerBsTbRiskHazardsStdDetailDeleteByIdIdUsingPost } from "@/genapi/wh";

import { injectState } from "../state";

import bus from "vue3-eventbus";

defineOptions({ name: "Sheet" });

const {
	selectedList,
	tableRef,
	detailDialogRef,
	page: { records, isLoading, execute },
} = injectState()!;

bus.on("updateTableData", (val) => {
	records.value = val;
});

watch(records, () => {
	tableRef.value?.table?.clearSelection();
});

const onDelete = async (row: (typeof records.value)[0]) => {
	await ElMessageBox.confirm(
		`您即将删除【${row.name ?? "未知"}】，删除后不可恢复，是否确认删除？`,
		{
			type: "error",
			title: "提示",
		},
	);
	await whManagerBsTbRiskHazardsStdDetailDeleteByIdIdUsingPost({
		path: { id: row.id! },
	});
	ElMessage.success("删除成功");
	await execute();
};

const columns: TableColumnOption<(typeof records.value)[0]>[] = [
	{
		label: "序号",
		type: "index",
		fixed: "left",
		width: 80,
	},

	{
		label: "检查内容",
		formatter: (row) => row.content ?? "-",
	},
	{
		label: "检查依据",
		formatter: (row) => row.checkDependent ?? "-",
	},
	{
		label: "操作",
		export: false,
		fixed: "right",
		width: "200",
		noExport: true,
		formatter(row) {
			return (
				<>
					<el-button
						size="small"
						type="primary"
						text
						onClick={() => detailDialogRef.value?.trigger("write", row, false)}
					>
						编辑
					</el-button>
					<el-button
						size="small"
						type="primary"
						text
						onClick={() => onDelete(row)}
					>
						删除
					</el-button>
				</>
			);
		},
	},
];

onBeforeUnmount(() => {
	bus.off("updateTableData");
});
</script>

<template>
	<TableWidget
		ref="tableRef"
		v-loading="isLoading"
		:data="records"
		:columns="columns"
		h="100%!"
		@selection-change="(rows:any) => (selectedList = toRaw(rows))"
	/>
</template>
